package com.gxc.datacenter.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.datacore.metadata.domain.Table;
import com.gxc.datacore.metadata.service.TableService;
import com.gxc.datacore.query.service.DataAccessService;
import com.gxc.datacore.query.service.DataQueryService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

@Controller("datacenter/dataQueryController")
public class DataQueryController {

	@Autowired
	private TableService tableService;
	@Autowired
	private DataAccessService dataAccessService;
	@Autowired
	private DataQueryService dataQueryService;

	/**
	 * 查询服务列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = new HashMap();
		List tableList = dataAccessService
				.getTableList(CurrentUtil.getUserId());
		model.put("tableList", tableList);
		return new ModelAndView("datacenter/dataquery/list.vm", model);
	}

	/**
	 * 查询页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = new HashMap();
		String table_id = request.getParameter("QueryObjectId");
		if (StringUtil.isTrimEmpty(table_id)) {
			return this.list(request, response);
		} else {
			model.put("QueryObjectId", table_id);
			Table table = tableService.retrieve(table_id);
			model.put("table", table);
			List columns = dataAccessService.getColumnList(
					CurrentUtil.getUserId(), table_id);
			model.put("columns", columns);
			return new ModelAndView("datacenter/dataquery/query.vm", model);
		}
	}

	/**
	 * ajax 查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView queryGrid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String table_id = request.getParameter("QueryObjectId");
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		Map params = RequestUtil.getInMap(request);
		PageList pageList = dataQueryService.query(CurrentUtil.getUserId(),
				table_id, params, true, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}
}
